<template>
  <div>
    <div>
      <el-divider content-position="left">资源查询</el-divider>
      <el-form ref="form" :model="form" label-width="80px" class="">
        <el-form-item class="form-item" label="资源标识">
          <el-input v-model="form.name" placeholder="资源标识"></el-input>
        </el-form-item>
        <el-form-item class="form-item" label="资源路径">
          <el-input v-model="form.url" placeholder="资源路径"></el-input>
        </el-form-item>
        <el-form-item class="form-item" label="资源类型" content="">
          <el-select v-model="form.type" placeholder="资源类型">
            <el-option label="接口资源" value="JK"></el-option>
            <el-option label="菜单资源" value="CD"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="form-item">
          <el-button class="form-btn" type="primary" @click="onQuery">查询</el-button>
          <el-button class="form-btn" @click="clearQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-divider content-position="left">资源列表</el-divider>
    <!--增改删导出-->
    <el-row>
      <el-button class="tag-btn" type="success" plain @click="addResVisible = true">新增</el-button>
      <el-button class="tag-btn" type="warning" plain @click="modifyItf('modify')">修改</el-button>
      <el-button class="tag-btn" type="danger" plain @click="delItf('del')">删除</el-button>
      <el-button class="tag-btn" type="info" plain @click="outputItfData">导出</el-button>
      <template>
        <el-input-number class="curPageCounter" @change="handleCurrentChange" v-model="curPage" :min="1" :max="pages" label="描述文字"></el-input-number>
      </template>
    </el-row>

    <!--列表-->
    <template>
      <el-table
        class="interface-table"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          align="center"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          align="center"
          prop="resId"
          label="资源ID"
          width="70">
        </el-table-column>
        <el-table-column
          align="center"
          prop="resName"
          label="资源标识"
          width="200">
        </el-table-column>
        <el-table-column
          align="center"
          prop="resUrl"
          label="资源路径"
          width="250">
        </el-table-column>
        <el-table-column
          align="center"
          prop="resType"
          label="资源类型"
          width="80">
        </el-table-column>
        <el-table-column
          align="center"
          prop="resDesc"
          label="资源描述"
          width="200">
        </el-table-column>
        <el-table-column
          align="center"
          prop="enabled"
          label="是否启用"
          width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              @change="handleResSwithEnable(scope)"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="createTime"
          label="创建时间"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          prop="updateTime"
          label="更新时间"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button
              v-show="scope.row.enabled"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination
          background
          class="interpage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="curPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </template>
    <!--新增资源对话框-->
    <el-dialog
      title="新增资源"
      :visible.sync="addResVisible"
      width="40%">
      <el-form label-width="80px">
        <el-form-item label="资源名称">
          <el-input v-model="addResForm.name" placeholder="请输入资源名称"></el-input>
        </el-form-item>
        <el-form-item label="资源URL">
          <el-input v-model="addResForm.url" placeholder="请输入资源路径"></el-input>
        </el-form-item>
        <el-form-item label="资源类型">
          <el-select v-model="addResForm.type" :placeholder="addResForm.type">
            <el-option label="接口资源" value="JK"></el-option>
            <el-option label="菜单资源" value="CD"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源描述">
          <el-input
            style="font-size: 16px;"
            type="textarea"
            :rows="5"
            placeholder='请输入资源描述'
            v-model="addResForm.desc">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addItfVisible = false">取 消</el-button>
        <el-button type="primary" @click="addItf">提交</el-button>
      </span>
    </el-dialog>

    <!--修改资源对话框-->
    <el-dialog
      title="修改资源"
      :visible.sync="modifyResVisible"
      width="40%">
      <el-form label-width="80px">
        <el-form-item label="资源名称">
          <el-input v-model="modifyResForm.name" :placeholder="modifyResForm.name" disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="资源URL">
          <el-input v-model="modifyResForm.url" :placeholder="modifyResForm.url" disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="资源类型">
          <el-select v-model="modifyResForm.type" :placeholder="modifyResForm.type" disabled="disabled">
            <el-option label="接口资源" value="JK"></el-option>
            <el-option label="菜单资源" value="CD"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源描述">
          <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入资源描述"
            v-model="modifyResForm.desc" :placeholder="modifyResForm.desc">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyItfVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModifyItf">提交</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: "resource",
    props: {
    },
    data() {
      return {
        /*查询表单*/
        form: {
          name: '',
          url: '',
          type: '',
        },
        /*接口列表*/
        tableData: [],
        multipleSelection: [],
        /*分页*/
        curPage: 1,
        size: 5,
        total: 0,
        pages: 0,
        addResVisible: false,
        addResForm: {
          name: '',
          url: '',
          desc: '',
          type: ''
        },
        modifyResVisible: false,
        modifyResForm: {
          name: '',
          url: '',
          type: '',
          desc: '',
        },
      }
    },
    mounted: function(){
      this.queryResList();
    },
    computed: {
      headers() {
        return {
          Authentication: this.$store.state.token,
          RefreshToken: this.$store.state.refresh_token
        };
      }
    },
    methods: {
      handleResSwithEnable(scope){
        this.$http.post("/res/info/isEnable", {
          "resId": scope.row.resId,
          "isEnabled": scope.row.enabled
        }).then(resp => {
          if(resp.data.errCode === 200){
            if(scope.row.enabled){
              this.$message.success("资源["+scope.row.resName+"]已启用");
            }else{
              this.$message.success("资源["+scope.row.resName+"]已停用");
            }
          }
        })
      },
      resetInterfaceQueryForm(form) {
        this.$refs[form].resetFields();
      },
      //接口查询
      onQuery(){
        this.queryResList();
        console.log("查询成功！");
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleEdit(index, row) {
        console.log(index, row);
        this.modifyItf(row.resId);
      },
      handleDelete(index, row) {
        console.log(index, row);
        if(confirm("确认要删除？")){
          this.delItf(row.resId);
        };
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.size = val;
        this.queryResList();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.curPage = val;
        this.queryResList();
      },
      //提交新增资源
      addItf(){
        console.log("提交成功！");
        this.$http.post("/res/info/add", {
          "resName": this.addResForm.name,
          "resUrl": this.addResForm.url,
          "resDesc": this.addResForm.desc,
          "resType": this.addResForm.type
        }).then(resp => {
          if(resp.data.errCode === 200){
            this.$message.success("新增成功！");
            this.addResVisible = false;
            this.queryResList();
            this.clearAddResForm();
          }
        });
      },
      //修改按钮(oprNum操作数为1时则为“修改”功能，否则为编辑)
      modifyItf(oprNum){
        //修改
        if(oprNum === 'modify'){
          //判断是否已选择
          if(this.multipleSelection.length !== 1){
            this.$message.error("请选择一条资源数据！");
            return;
          }
          this.modifyResVisible = true;
          this.multipleSelection.forEach(t => {
            this.queryOne(t.resId);
          });
        }else { //编辑
          this.queryOne(oprNum);
        }
      },
      //删除
      delItf(oprNum){
        //删除按钮
        if(oprNum === 'del'){
          //判断是否已选择
          if(this.multipleSelection.length !== 1){
            this.$message.error("请选择一条资源数据！");
            return;
          }
          if(confirm("确认要删除？")){
            this.multipleSelection.forEach(t => {
              this.submitDelItf(t.resId);
            });
          }
        }else { //操作中的删除
          this.submitDelItf(oprNum);
        }
      },

      //修改接口
      submitModifyItf(){
        this.$http.post("/res/info/mod", {
          "resName": this.modifyResForm.name,
          "resUrl": this.modifyResForm.url,
          "resType": this.modifyResForm.type,
          "resDesc": this.modifyResForm.desc
        }).then(resp => {
          if(resp.data.errCode === 200){
            this.$message.success("资源修改成功！");
            this.modifyResVisible = false;
            this.queryResList();
          }
        })
      },

      //导出接口数据
      outputItfData(){
        //判断是否已选择
        if(this.multipleSelection.length === 0){
          this.$message.error("请选择一条资源数据！");
          return;
        }
        this.$http({
          method: "post",
          url: "/mock/interface/export",
          data: this.multipleSelection,
          responseType: "blob"
        }).then(resp => {
          let url = window.URL.createObjectURL(new Blob([resp.data])); //创建blob,设置文件类型
          let a = document.createElement('a'); //创建a标签
          a.download = "接口数据表.xls"; //指定文件名
          a.style.display = 'none';
          a.href = url; //创建url
          a.click(); //click事件触发下载
          // URL.revokeObjectURL(a); //释放内存
        });
      },

      //查询接口列表
      queryResList(){
        this.$http.post("/res/info/query", {
          "curPage": this.curPage,
          "size": this.size,
          "resName": this.form.name,
          "resUrl": this.form.url,
          "resType": this.form.type
        }).then(resp => {
          if (resp.data.errCode === 200){
            this.total = resp.data.data.totals;
            this.pages = resp.data.data.pages;
            this.tableData = resp.data.data.data;
          }
        })
      },

      //查询单个接口
      queryOne(id){
        this.$http.post("/res/info/query/"+id).then(resp => {
          if(resp.data.errCode === 200){
            this.modifyResVisible = true;
            this.modifyResForm.name = resp.data.data.resName;
            this.modifyResForm.url = resp.data.data.resUrl;
            this.modifyResForm.type = resp.data.data.resType;
            this.modifyResForm.desc = resp.data.data.resDesc;
          }
        })
      },

      //删除单个接口
      submitDelItf(id){
        this.$http.post("/res/info/delete/"+id).then(resp => {
          if(resp.data.errCode === 200){
            this.$message.success("删除成功！");
            this.queryResList();
          }
        })
      },

      //清空查询条件
      clearQuery(){
        this.form = {
          name: '',
          url: '',
          type: ''
        }
      },
      clearAddResForm(){
        this.addResForm = {
          name: '',
          url: '',
          desc: '',
        }
      },
      checkJsonArray(respParams) {
        if(respParams === '' || respParams === null){
          this.$message.error("请输入正确格式的响应规则！");
          return;
        }

      }
    }
  }
</script>

<style scoped>
  .form-item{
    margin-left: 20px;
    width: 20%;
    display: inline-block;
  }
  .form-btn{
    position: relative;
    left: -60px;
  }
  .tag-btn{
    margin-left: 20px;
  }
  .interface-table{
    position: relative;
    left: 20px;
    top:10px;
  }
  .interpage{
    margin-top: 20px;
    margin-left: 20px;
    margin-bottom: 20px;
  }
  .curPageCounter{
    float: right;
    right: 40px;
  }
</style>
